package com.achievo.know.application.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.achievo.know.application.FrontApplication;
import com.achievo.know.domain.Area;
import com.achievo.know.domain.AreaRepository;
import com.achievo.know.domain.News;
import com.achievo.know.domain.NewsRepository;
import com.achievo.know.pojo.SearchResult;
import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;

@Service
public class FrontApplicationImpl implements FrontApplication {
	
	
	
	@Override
	public List<News> findNewsByArea(List<Long> cs,Long type,int limit) {
		return newsRespository.findNewsByArea(cs,type,limit);
	}
	
	@Override
	public Area getArea(Long id) {
		return areaRepository.get(id);
	}


	@Override
	public Page<SearchResult> search(String searchText, int currentPage, int pageSize) {
		long total=newsRespository.searchCount(searchText);
		List<Object> result=newsRespository.search(searchText,currentPage,pageSize);
		List<SearchResult> list=new ArrayList<SearchResult>();
		for(Object mm:result){
			SearchResult s=new SearchResult();
			Object[] m=(Object[])mm;
			if(m[0]!=null)
			s.setTitle(m[0].toString());
			if(m[1]!=null)
			s.setDesc(m[1].toString());
			if(m[2]!=null){
			Date d;
			try {
				d = DateUtils.parseDate(m[2].toString(),new String[]{"yyyy-MM-dd HH:mm:ss.SSS"});
				s.setPublishTime(d);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			if(m[3]!=null)
			s.setPublishUser(m[3].toString());
			if(m[4]!=null)
			s.setBrowserNum(NumberUtils.toInt(m[4].toString()));
			if(m[5]!=null)
			s.setDownSum(NumberUtils.toInt(m[5].toString()));
			if(m[6]!=null)
			s.setFile(NumberUtils.toInt(m[6].toString())>0?true:false);
			if(m[7]!=null)
				s.setId(NumberUtils.createLong(m[7].toString()));
			list.add(s);
		}
		Page<SearchResult> page=new Page<SearchResult>((currentPage - 1) * pageSize, total,pageSize,list);
		return page;
	}

	@Override
	public List<Area> listArea() {
		return areaRepository.findAll();
	}
	
	@Override
	@Transactional
	public void saveArea(Area temp) {
		areaRepository.save(temp);
	}
	
	@Override
	public Page<News> findPageNewsByArea(List<Long> cateIds,Long type, int currentPage,
			int pageSize) {
		List<News> list= newsRespository.findPageNewsByArea(cateIds,type,currentPage,pageSize);
		long total=newsRespository.countNewsByArea(cateIds,type);
		Page<News> page=new Page<News>((currentPage - 1) * pageSize,total,pageSize,list);
		return page;
	}
	
	@Override
	public Page<SearchResult> findByCate(String orderType, String resType,List<Long> cateIds, int currentPage,
			int pageSize) {
		long total=newsRespository.countByCate(orderType,resType,cateIds);
		if(total<=0){
			return new Page<SearchResult>((currentPage - 1) * pageSize,total,pageSize,new ArrayList());
		}
		List<SearchResult> list= newsRespository.findPageNewsByCate(orderType,resType,cateIds,currentPage,pageSize);
		Page<SearchResult> page=new Page<SearchResult>((currentPage - 1) * pageSize,total,pageSize,list);
		return page;
	}
	

	@Autowired
	private NewsRepository newsRespository;
	@Autowired
	private AreaRepository areaRepository;
	@Autowired
	private QueryChannelService queryChannel;
	

	public AreaRepository getAreaRepository() {
		return areaRepository;
	}

	public void setAreaRepository(AreaRepository areaRepository) {
		this.areaRepository = areaRepository;
	}

	public NewsRepository getNewsRespository() {
		return newsRespository;
	}

	public void setNewsRespository(NewsRepository newsRespository) {
		this.newsRespository = newsRespository;
	}
	
	

}
